//
//  PersonalVipViewModel.m
//  ChatForHot
//
//  Created by wr on 2018/10/11.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "PersonalVipViewModel.h"
#import "PersonalVipInfoModel.h"
#import "PersonalCurrentUserVipModel.h"

@interface PersonalVipViewModel ()

@property (nonatomic, strong, readwrite) RACCommand *requestVipInfoCommand;
@property (nonatomic, strong, readwrite) RACCommand *cancelRequestVipInfoCommand;

@property (nonatomic, strong, readwrite) RACCommand *requestCurrentUserVipInfoCommand;
@property (nonatomic, strong, readwrite) RACCommand *cancelRequestCurrentUserVipInfoCommand;

@property (nonatomic, strong, readwrite) NSMutableArray <PersonalVipCellModel *> *sectionOneCellDataSource;
@property (nonatomic, strong, readwrite) NSMutableArray <PersonalVipCellModel *> *sectionTwoCellDataSource;
@property (nonatomic, strong, readwrite) NSArray *dataArray;
@property (nonatomic, strong, readwrite) NSArray *cuserVipInfoArray;
@end

@implementation PersonalVipViewModel

- (void)HOTCHATdidInitialized {
    [super HOTCHATdidInitialized];
    
    self.dataArray = @[];
    self.cuserVipInfoArray = @[];
    @weakify(self)
    self.cancelRequestVipInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [RACSignal empty];
    }];
    
    self.requestVipInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
            [parameters setObject:[APPINFO defaultManager].appid forKey:@"appid"];
            [NetWorkTool HOTCHATRequestWithStringURL:PurchaseInfo_URL progress:^(NSProgress * _Nonnull progress) {
            } RequestWay:@"GET" Parma:parameters SuccessBlock:^(BOOL isSucess, NSDictionary * _Nonnull resultDictionary) {
                @strongify(self)
                if (isSucess) {
                    [self processInfoData:resultDictionary];
                    [subscriber sendNext:self.dataArray];
                }
                [subscriber sendCompleted];
            } FailureBlock:^(NSError * _Nonnull error) {
                [subscriber sendError:error];
            }];
            return [RACDisposable disposableWithBlock:^{
            }];
        }] materialize] takeUntil:self.cancelRequestVipInfoCommand.executionSignals];
    }];
    
    self.cancelRequestCurrentUserVipInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [RACSignal empty];
    }];
    
    self.requestCurrentUserVipInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
            [parameters setObject:[APPINFO defaultManager].appid forKey:@"appid"];
            [parameters setObject:[APPINFO defaultManager].accountid forKey:@"accountid"];
            [NetWorkTool HOTCHATRequestWithStringURL:CurentUserVipInfo_URL progress:^(NSProgress * _Nonnull progress) {
            } RequestWay:@"GET" Parma:parameters SuccessBlock:^(BOOL isSucess, NSDictionary * _Nonnull resultDictionary) {
                @strongify(self)
                if (isSucess) {
                    [self HOTCHATprocessCurrentUserInfoData:resultDictionary];
                    [subscriber sendNext:self.dataArray];
                }
                [subscriber sendCompleted];
            } FailureBlock:^(NSError * _Nonnull error) {
                [subscriber sendError:error];
            }];
            return [RACDisposable disposableWithBlock:^{
            }];
        }] materialize] takeUntil:self.cancelRequestVipInfoCommand.executionSignals];
    }];
}

- (void)processInfoData:(NSDictionary *)dic {
    NSArray *array = dic[@"data"];
    __block NSMutableArray *relDataArray = @[].mutableCopy;
    [array enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        PersonalVipInfoModel *model = [[PersonalVipInfoModel alloc] initWithDictionary:obj];
        if (idx == 0) {
            model.selected = YES;
        }else {
            model.selected = NO;
        }
        [relDataArray addObject:model];
    }];
    
    self.dataArray = [NSArray arrayWithArray:relDataArray];
}

- (void)HOTCHATprocessCurrentUserInfoData:(NSDictionary *)dic {
    __block NSMutableArray *relDataArray = @[].mutableCopy;
    PersonalCurrentUserVipModel *model = [[PersonalCurrentUserVipModel alloc] initWithDictionary:dic[@"data"]];
    [relDataArray addObject:model];
    self.cuserVipInfoArray = [NSArray arrayWithArray:relDataArray];
}

- (NSMutableArray<PersonalVipCellModel *> *)sectionOneCellDataSource {
    NSMutableArray *cellDataSource = [[NSMutableArray alloc] init];
    PersonalVipCellModel *one = [[PersonalVipCellModel alloc] init];
    one.title = NSLocalizedString(@"personalVIP_11", nil);
    one.icon = @"p_vip_ic1";
    one.tag = 0;
    one.titleTextLayout = [self textLayoutWithString:one.title];
    one.cellHeight = 44;
    
    PersonalVipCellModel *two = [[PersonalVipCellModel alloc] init];
    two.title = NSLocalizedString(@"personalVIP_12", nil);
    two.icon = @"p_vip_ic2";
    two.tag = 1;
    two.titleTextLayout = [self textLayoutWithString:two.title];
    two.cellHeight = 44;
    
    PersonalVipCellModel *the = [[PersonalVipCellModel alloc] init];
    the.title = NSLocalizedString(@"personalVIP_13", nil);
    the.icon = @"p_vip_ic3";
    the.tag = 2;
    the.titleTextLayout = [self textLayoutWithString:the.title];
    the.cellHeight = 44;
    
    PersonalVipCellModel *four = [[PersonalVipCellModel alloc] init];
    four.title = NSLocalizedString(@"personalVIP_14", nil);
    four.icon = @"p_vip_ic4";
    four.tag = 3;
    four.titleTextLayout = [self textLayoutWithString:four.title];
    four.cellHeight = 44;
    
    PersonalVipCellModel *five = [[PersonalVipCellModel alloc] init];
    five.title = NSLocalizedString(@"personalVIP_15", nil);
    five.icon = @"p_vip_ic5";
    five.tag = 4;
    five.titleTextLayout = [self textLayoutWithString:five.title];
    five.cellHeight = 44;
    
    PersonalVipCellModel *six = [[PersonalVipCellModel alloc] init];
    six.title = NSLocalizedString(@"personalVIP_16", nil);
    six.icon = @"p_vip_ic6";
    six.tag = 5;
    six.titleTextLayout = [self textLayoutWithString:six.title];
    six.cellHeight = 44;
    
    PersonalVipCellModel *seven = [[PersonalVipCellModel alloc] init];
    seven.title = NSLocalizedString(@"personalVIP_17", nil);
    seven.icon = @"p_vip_ic7";
    seven.tag = 6;
    seven.titleTextLayout = [self textLayoutWithString:seven.title];
    seven.cellHeight = 44;
    
    PersonalVipCellModel *eight = [[PersonalVipCellModel alloc] init];
    eight.title = NSLocalizedString(@"personalVIP_18", nil);
    eight.icon = @"p_vip_ic8";
    eight.tag = 7;
    eight.titleTextLayout = [self textLayoutWithString:eight.title];
    eight.cellHeight = 44;
    
    [cellDataSource addObjectsFromArray:@[one,two,four,five,six]];
    return cellDataSource;
}

- (NSMutableArray<PersonalVipCellModel *> *)sectionTwoCellDataSource {
    NSMutableArray *cellDataSource = [[NSMutableArray alloc] init];
    PersonalVipCellModel *one = [[PersonalVipCellModel alloc] init];
    one.title = NSLocalizedString(@"personalVIP_01", nil);
    one.icon = @"all_ic_prompt";
    one.tag = 8;
    one.titleTextLayout = [self textLayoutWithString:one.title];
    one.cellHeight = one.titleTextLayout.textBoundingSize.height + 20;
    
    PersonalVipCellModel *two = [[PersonalVipCellModel alloc] init];
    two.title = NSLocalizedString(@"personalVIP_02", nil);
    two.icon = @"all_ic_prompt";
    two.tag = 9;
    two.titleTextLayout = [self textLayoutWithString:two.title];
    two.cellHeight = two.titleTextLayout.textBoundingSize.height + 20;
    
    PersonalVipCellModel *four = [[PersonalVipCellModel alloc] init];
    four.icon = @"all_ic_prompt";
    four.title = NSLocalizedString(@"personalVIP_04", nil);
    four.tag = 10;
    four.titleTextLayout = [self textLayoutWithString:four.title];
    four.cellHeight = four.titleTextLayout.textBoundingSize.height + 20;
    
    PersonalVipCellModel *five = [[PersonalVipCellModel alloc] init];
    five.title = NSLocalizedString(@"personalVIP_05", nil);
    five.icon = @"all_ic_prompt";
    five.tag = 11;
    five.titleTextLayout = [self textLayoutWithString:five.title];
    five.cellHeight = five.titleTextLayout.textBoundingSize.height + 20;
    
    PersonalVipCellModel *six = [[PersonalVipCellModel alloc] init];
    six.title = NSLocalizedString(@"personalVIP_06", nil);
    six.icon = @"all_ic_prompt";
    six.tag = 12;
    six.titleTextLayout = [self textLayoutWithString:six.title];
    six.cellHeight = six.titleTextLayout.textBoundingSize.height + 20;
    
    PersonalVipCellModel *seven = [[PersonalVipCellModel alloc] init];
    seven.title = NSLocalizedString(@"personalVIP_07", nil);
    seven.icon = @"all_ic_prompt";
    seven.tag = 13;
    seven.titleTextLayout = [self textLayoutWithString:seven.title];
    seven.cellHeight = seven.titleTextLayout.textBoundingSize.height + 20;
    
    PersonalVipCellModel *help1 = [[PersonalVipCellModel alloc] init];
    help1.title = NSLocalizedString(@"personalVIP_08", nil);
    help1.icon = @"all_ic_prompt";
    help1.tag = 14;
    help1.titleTextLayout = [self textLayoutWithString:help1.title];
    help1.cellHeight = help1.titleTextLayout.textBoundingSize.height + 20;
    help1.handler = ^{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://sites.google.com/view/chatforhotpolicy"]];
    };
    
    PersonalVipCellModel *help2 = [[PersonalVipCellModel alloc] init];
    help2.title = NSLocalizedString(@"personalVIP_09", nil);
    help2.icon = @"all_ic_prompt";
    help2.tag = 15;
    help2.titleTextLayout = [self textLayoutWithString:help1.title];
    help2.cellHeight = help2.titleTextLayout.textBoundingSize.height + 20;
    help2.handler = ^{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://sites.google.com/view/chatforhotterms"]];
    };
    
    [cellDataSource addObjectsFromArray:@[one,two,four,five,six,seven,help1,help2]];
    return cellDataSource;
}

- (YYTextLayout *)textLayoutWithString:(NSString *)str {
    NSString *title = str;
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:title];
    text.font = [UIFont systemFontOfSize:11.0];
    text.lineSpacing = 3;
    YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(kScreenWidth -70 , MAXFLOAT)];
    container.maximumNumberOfRows = 0;
    YYTextLayout *textLayout = [YYTextLayout layoutWithContainer:container text:text];
    return textLayout;
}

@end
